package com.canqiang.contact;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class MemberAdapter  extends BaseExpandableListAdapter  {
	private Context context;
    private ArrayList<String> groups;
    private ArrayList<ArrayList<Member>> members;
    private LayoutInflater inflater;
    public MemberAdapter(Context context, 
			            ArrayList<String> groups,
						ArrayList<ArrayList<Member>> members ) { 
			this.context = context;
			this.groups = groups;
			this.members = members;
			inflater = LayoutInflater.from( context );
			}
    public Object getChild(int groupPosition, int childPosition) {
        return members.get( groupPosition ).get( childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_row, parent, false); 
        Member c = (Member)getChild( groupPosition, childPosition );
		TextView member = (TextView)v.findViewById( R.id.childname );
		if( member != null )
			member.setText( c.getName());
		TextView a=(TextView)v.findViewById( R.id.mobile );
		a.setText(c.getMobile());
		TextView b=(TextView)v.findViewById( R.id.phone );
		b.setText(c.getPhone());
		CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        cb.setChecked( c.getState() );
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return members.get( groupPosition ).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get( groupPosition );        
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
		TextView memberGroup = (TextView)v.findViewById( R.id.childname );
		if( gt != null )
			memberGroup.setText( gt );
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {} 
    public void onGroupExpanded(int groupPosition) {}


}
